using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace MyCityCab.Core
{
	/// <summary>
	/// Strongly-typed collection for the Location class.
	/// </summary>
	[Serializable]
	public partial class LocationCollection : ActiveList<Location, LocationCollection> 
	{	   
		public LocationCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Location table.
	/// </summary>
	[Serializable]
	public partial class Location : ActiveRecord<Location>
	{
		#region .ctors and Default Settings
		
		public Location()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Location(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Location(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Location(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Location", TableType.Table, DataService.GetInstance("MyCityCab"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarId = new TableSchema.TableColumn(schema);
				colvarId.ColumnName = "Id";
				colvarId.DataType = DbType.Int32;
				colvarId.MaxLength = 0;
				colvarId.AutoIncrement = true;
				colvarId.IsNullable = false;
				colvarId.IsPrimaryKey = true;
				colvarId.IsForeignKey = false;
				colvarId.IsReadOnly = false;
				colvarId.DefaultSetting = @"";
				colvarId.ForeignKeyTableName = "";
				schema.Columns.Add(colvarId);
				
				TableSchema.TableColumn colvarLocationTypeId = new TableSchema.TableColumn(schema);
				colvarLocationTypeId.ColumnName = "LocationType_Id";
				colvarLocationTypeId.DataType = DbType.Int32;
				colvarLocationTypeId.MaxLength = 0;
				colvarLocationTypeId.AutoIncrement = false;
				colvarLocationTypeId.IsNullable = false;
				colvarLocationTypeId.IsPrimaryKey = false;
				colvarLocationTypeId.IsForeignKey = true;
				colvarLocationTypeId.IsReadOnly = false;
				colvarLocationTypeId.DefaultSetting = @"";
				
					colvarLocationTypeId.ForeignKeyTableName = "LocationType";
				schema.Columns.Add(colvarLocationTypeId);
				
				TableSchema.TableColumn colvarCityId = new TableSchema.TableColumn(schema);
				colvarCityId.ColumnName = "City_Id";
				colvarCityId.DataType = DbType.Int32;
				colvarCityId.MaxLength = 0;
				colvarCityId.AutoIncrement = false;
				colvarCityId.IsNullable = false;
				colvarCityId.IsPrimaryKey = false;
				colvarCityId.IsForeignKey = true;
				colvarCityId.IsReadOnly = false;
				colvarCityId.DefaultSetting = @"";
				
					colvarCityId.ForeignKeyTableName = "City";
				schema.Columns.Add(colvarCityId);
				
				TableSchema.TableColumn colvarRegionId = new TableSchema.TableColumn(schema);
				colvarRegionId.ColumnName = "Region_Id";
				colvarRegionId.DataType = DbType.Int32;
				colvarRegionId.MaxLength = 0;
				colvarRegionId.AutoIncrement = false;
				colvarRegionId.IsNullable = true;
				colvarRegionId.IsPrimaryKey = false;
				colvarRegionId.IsForeignKey = true;
				colvarRegionId.IsReadOnly = false;
				colvarRegionId.DefaultSetting = @"";
				
					colvarRegionId.ForeignKeyTableName = "Region";
				schema.Columns.Add(colvarRegionId);
				
				TableSchema.TableColumn colvarEnglishName = new TableSchema.TableColumn(schema);
				colvarEnglishName.ColumnName = "EnglishName";
				colvarEnglishName.DataType = DbType.String;
				colvarEnglishName.MaxLength = 50;
				colvarEnglishName.AutoIncrement = false;
				colvarEnglishName.IsNullable = false;
				colvarEnglishName.IsPrimaryKey = false;
				colvarEnglishName.IsForeignKey = false;
				colvarEnglishName.IsReadOnly = false;
				colvarEnglishName.DefaultSetting = @"";
				colvarEnglishName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarEnglishName);
				
				TableSchema.TableColumn colvarLocalName = new TableSchema.TableColumn(schema);
				colvarLocalName.ColumnName = "LocalName";
				colvarLocalName.DataType = DbType.String;
				colvarLocalName.MaxLength = 50;
				colvarLocalName.AutoIncrement = false;
				colvarLocalName.IsNullable = true;
				colvarLocalName.IsPrimaryKey = false;
				colvarLocalName.IsForeignKey = false;
				colvarLocalName.IsReadOnly = false;
				colvarLocalName.DefaultSetting = @"";
				colvarLocalName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLocalName);
				
				TableSchema.TableColumn colvarLongtitude = new TableSchema.TableColumn(schema);
				colvarLongtitude.ColumnName = "Longtitude";
				colvarLongtitude.DataType = DbType.Decimal;
				colvarLongtitude.MaxLength = 0;
				colvarLongtitude.AutoIncrement = false;
				colvarLongtitude.IsNullable = true;
				colvarLongtitude.IsPrimaryKey = false;
				colvarLongtitude.IsForeignKey = false;
				colvarLongtitude.IsReadOnly = false;
				colvarLongtitude.DefaultSetting = @"";
				colvarLongtitude.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLongtitude);
				
				TableSchema.TableColumn colvarLatitude = new TableSchema.TableColumn(schema);
				colvarLatitude.ColumnName = "Latitude";
				colvarLatitude.DataType = DbType.Decimal;
				colvarLatitude.MaxLength = 0;
				colvarLatitude.AutoIncrement = false;
				colvarLatitude.IsNullable = true;
				colvarLatitude.IsPrimaryKey = false;
				colvarLatitude.IsForeignKey = false;
				colvarLatitude.IsReadOnly = false;
				colvarLatitude.DefaultSetting = @"";
				colvarLatitude.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLatitude);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["MyCityCab"].AddSchema("Location",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("Id")]
		public int Id 
		{
			get { return GetColumnValue<int>("Id"); }

			set { SetColumnValue("Id", value); }

		}

		  
		[XmlAttribute("LocationTypeId")]
		public int LocationTypeId 
		{
			get { return GetColumnValue<int>("LocationType_Id"); }

			set { SetColumnValue("LocationType_Id", value); }

		}

		  
		[XmlAttribute("CityId")]
		public int CityId 
		{
			get { return GetColumnValue<int>("City_Id"); }

			set { SetColumnValue("City_Id", value); }

		}

		  
		[XmlAttribute("RegionId")]
		public int? RegionId 
		{
			get { return GetColumnValue<int?>("Region_Id"); }

			set { SetColumnValue("Region_Id", value); }

		}

		  
		[XmlAttribute("EnglishName")]
		public string EnglishName 
		{
			get { return GetColumnValue<string>("EnglishName"); }

			set { SetColumnValue("EnglishName", value); }

		}

		  
		[XmlAttribute("LocalName")]
		public string LocalName 
		{
			get { return GetColumnValue<string>("LocalName"); }

			set { SetColumnValue("LocalName", value); }

		}

		  
		[XmlAttribute("Longtitude")]
		public decimal? Longtitude 
		{
			get { return GetColumnValue<decimal?>("Longtitude"); }

			set { SetColumnValue("Longtitude", value); }

		}

		  
		[XmlAttribute("Latitude")]
		public decimal? Latitude 
		{
			get { return GetColumnValue<decimal?>("Latitude"); }

			set { SetColumnValue("Latitude", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public MyCityCab.Core.RouteCollection RouteRecords()
		{
			return new MyCityCab.Core.RouteCollection().Where(Route.Columns.FromLocationId, Id).Load();
		}

		public MyCityCab.Core.RouteCollection RouteRecordsFromLocation()
		{
			return new MyCityCab.Core.RouteCollection().Where(Route.Columns.ToLocationId, Id).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a City ActiveRecord object related to this Location
		/// 
		/// </summary>
		public MyCityCab.Core.City City
		{
			get { return MyCityCab.Core.City.FetchByID(this.CityId); }

			set { SetColumnValue("City_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a LocationType ActiveRecord object related to this Location
		/// 
		/// </summary>
		public MyCityCab.Core.LocationType LocationType
		{
			get { return MyCityCab.Core.LocationType.FetchByID(this.LocationTypeId); }

			set { SetColumnValue("LocationType_Id", value.Id); }

		}

		
		
		/// <summary>
		/// Returns a Region ActiveRecord object related to this Location
		/// 
		/// </summary>
		public MyCityCab.Core.Region Region
		{
			get { return MyCityCab.Core.Region.FetchByID(this.RegionId); }

			set { SetColumnValue("Region_Id", value.Id); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(int varLocationTypeId,int varCityId,int? varRegionId,string varEnglishName,string varLocalName,decimal? varLongtitude,decimal? varLatitude)
		{
			Location item = new Location();
			
			item.LocationTypeId = varLocationTypeId;
			
			item.CityId = varCityId;
			
			item.RegionId = varRegionId;
			
			item.EnglishName = varEnglishName;
			
			item.LocalName = varLocalName;
			
			item.Longtitude = varLongtitude;
			
			item.Latitude = varLatitude;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varId,int varLocationTypeId,int varCityId,int? varRegionId,string varEnglishName,string varLocalName,decimal? varLongtitude,decimal? varLatitude)
		{
			Location item = new Location();
			
				item.Id = varId;
				
				item.LocationTypeId = varLocationTypeId;
				
				item.CityId = varCityId;
				
				item.RegionId = varRegionId;
				
				item.EnglishName = varEnglishName;
				
				item.LocalName = varLocalName;
				
				item.Longtitude = varLongtitude;
				
				item.Latitude = varLatitude;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string Id = @"Id";
			 public static string LocationTypeId = @"LocationType_Id";
			 public static string CityId = @"City_Id";
			 public static string RegionId = @"Region_Id";
			 public static string EnglishName = @"EnglishName";
			 public static string LocalName = @"LocalName";
			 public static string Longtitude = @"Longtitude";
			 public static string Latitude = @"Latitude";
						
		}

		#endregion
	}

}

